package com.miage.activity;

import com.miage.dao.DaoUtilisateur;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Authentification extends MainActivity {

	private EditText username = null;
	private EditText password = null;
	private TextView attempts;
	private Button login;
	int counter = 3;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.authentification);
		username = (EditText) findViewById(R.id.editText1);
		password = (EditText) findViewById(R.id.editText2);
		attempts = (TextView) findViewById(R.id.textView5);
		attempts.setText(Integer.toString(counter));
		login = (Button) findViewById(R.id.button1);
	}

	public void login(View view) {
		if ( username.getText() != null && password.getText() != null) {
			
			String inputPseudo = username.getText().toString();
			String inputMotDePasse = password.getText().toString();
			
			if (daoUtilisateur.isLogin(inputPseudo, inputMotDePasse) == null) {
				Toast.makeText(getApplicationContext(), "INVALIDE, DEGAGE !",
						Toast.LENGTH_SHORT).show();
				attempts.setBackgroundColor(Color.RED);
				counter--;
				attempts.setText(Integer.toString(counter));
				if (counter == 0) {
					login.setEnabled(false);
				}
			}
			else {
				Intent intent = new Intent(Authentification.this, MainActivity.class);
				startActivity(intent);	
			}
			
//			Toast.makeText(getApplicationContext(), "Redirecting...",
//					Toast.LENGTH_SHORT).show();
			
		} 
		else {
			Toast.makeText(getApplicationContext(), "Renseignez identifiant / mot de passe.",
					Toast.LENGTH_SHORT).show();
			attempts.setBackgroundColor(Color.RED);
			counter--;
			attempts.setText(Integer.toString(counter));
			if (counter == 0) {
				login.setEnabled(false);
			}
		}
	}
}
